package com.yvon.maple.service.msg.core.support.action;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Validator;
import com.alibaba.fastjson.JSON;
import com.yvon.boot.common.result.ResponseData;
import com.yvon.boot.common.result.ResultCode;
import com.yvon.maple.service.msg.core.common.domain.TaskInfo;
import com.yvon.maple.service.msg.core.common.enums.ChannelType;
import com.yvon.maple.service.msg.core.common.enums.IdType;
import com.yvon.maple.service.msg.core.common.model.send.SendTaskModel;
import com.yvon.maple.service.msg.core.support.pipeline.BusinessProcess;
import com.yvon.maple.service.msg.core.support.pipeline.ProcessContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 后置参数检查
 *
 * @author : Yvon
 * @since : 2021-12-23
 */
@Slf4j
@Service
public class AfterParamCheckAction implements BusinessProcess {


    @Override
    public void process(ProcessContext context) {

        SendTaskModel sendTaskModel = (SendTaskModel) context.getProcessModel();
        List<TaskInfo> taskInfo = sendTaskModel.getTaskInfo();

        // 1. 过滤掉不合法的手机号
        filterIllegalPhoneNum(taskInfo);

        // 2. 过滤掉不合法的邮箱
        filterIllegalEmail(taskInfo);

        if (CollUtil.isEmpty(taskInfo)) {
            context.setNeedBreak(true).setResponse(ResponseData.failed(ResultCode.CLIENT_BAD_PARAMETERS));
            return;
        }
    }

    private void filterIllegalPhoneNum(List<TaskInfo> taskInfo) {
        if (CollUtil.isEmpty(taskInfo)) {
            return;
        }
        IdType idType = taskInfo.get(0).getIdType();
        ChannelType channelType = taskInfo.get(0).getChannelType();

        if (IdType.PHONE.equals(idType) && ChannelType.SMS.equals(channelType)) {
            Iterator<TaskInfo> iterator = taskInfo.iterator();
            while (iterator.hasNext()) {
                TaskInfo task = iterator.next();
                Set<String> illegalPhone = task.getReceiver().stream().filter(phone -> !Validator.isMobile(phone))
                        .collect(Collectors.toSet());

                if (CollUtil.isNotEmpty(illegalPhone)) {
                    task.getReceiver().removeAll(illegalPhone);
                    log.error("{} find illegal phone!{}", task.getMessageTemplateId(), JSON.toJSONString(illegalPhone));
                }
                if (CollUtil.isEmpty(task.getReceiver())) {
                    iterator.remove();
                }
            }
        }

    }


    private void filterIllegalEmail(List<TaskInfo> taskInfo) {
        if (CollUtil.isEmpty(taskInfo)) {
            return;
        }
        IdType idType = taskInfo.get(0).getIdType();
        ChannelType channelType = taskInfo.get(0).getChannelType();

        if (IdType.EMAIL.equals(idType) && ChannelType.EMAIL.equals(channelType)) {
            Iterator<TaskInfo> iterator = taskInfo.iterator();
            while (iterator.hasNext()) {
                TaskInfo task = iterator.next();
                Set<String> illegalEmail = task.getReceiver().stream().filter(email -> !Validator.isEmail(email))
                        .collect(Collectors.toSet());

                if (CollUtil.isNotEmpty(illegalEmail)) {
                    task.getReceiver().removeAll(illegalEmail);
                    log.error("{} find illegal email!{}", task.getMessageTemplateId(), JSON.toJSONString(illegalEmail));
                }
                if (CollUtil.isEmpty(task.getReceiver())) {
                    iterator.remove();
                }
            }
        }
    }
}
